<?php

/**
 * Product category model
 *
 * @author NguyenVanTien
 */
class Application_Model_ProductCategories extends Application_Model_DbTable_ProductCategories {

    /**
     * Show list 
     * @return type 
     */
    public function getList() {
        $select = $this->select();
        $select->from('product_categories')
                ->where('parent = ?', '')
                ->orWhere('parent = ?', 0)
                ->orWhere('parent is null');
        
        $categories = $this->fetchAll($select)->toArray();
        return $this->getListLevel($categories);
    }

    /**
     * get list categories with level
     * @author Tien Nguyen
     * @param array $list 
     */
    private function getListLevel($list) {
        for($i= 0; $i < count($list); $i++){
            $select = $this->select();
            $select->from('product_categories')
                    ->where('parent = ?', $list[$i]['catalog_id']);
            
            $childs = $this->fetchAll($select)->toArray();
            $list[$i]['childs'] = $childs;
            
            if(count($childs) > 0){
                $this->getListLevel($childs);
            }
        }
        return $list;
    }
}